/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.liquidByte.trafficMustang.server.dao;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import org.liquidByte.trafficMustang.server.dataModel.Usuario;
import org.liquidByte.trafficMustang.server.repository.UsuarioRepository;

/**
 *
 * @author Alexandre
 */
@Stateless
public class UsuarioDAO extends PersistableDAO<Usuario> implements UsuarioRepository {

    public UsuarioDAO() {
        super(Usuario.class);
    }

    @Override
    public Usuario getByDocIdent(String docIdent) {
        EntityManager em = this.getEntityManager();
        CriteriaBuilder builder = em.getCriteriaBuilder();
        CriteriaQuery<Usuario> query = builder.createQuery(Usuario.class);
        Root<Usuario> root = query.from(Usuario.class);
        query.select(root);
        
        query.where(builder.equal(root.get("usrDocId"), docIdent));
        try {
            return em.createQuery(query).getSingleResult();
        } catch (NoResultException e) {
            return null;
        }        
    }
}
